<?php
/**
 * Groups module for XOOPS
 *
 * You may not change or alter any portion of this comment or credits
 * of supporting developers from this source code or any supporting source code 
 * which is considered copyrighted (c) material of the original comment or credit authors.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 * @copyright       The Groups Project http://code.google.com/p/groupsforxoops/
 * @license         http://www.fsf.org/copyleft/gpl.html GNU public license
 * @package         groups
 * @since           2.3.0
 * @author          Hui Xiao < xh.8326@gmail.com >
 * @version         $Id: event.php Sat Jul 18 13:15:36 CST 2009
 */

if ( !defined("XOOPS_ROOT_PATH") ) exit;
if ( !class_exists("xoGroups") ) {
	include dirname(dirname(__FILE__))."/include/groups.php";
}
class groupsEvent extends XoopsObject 
{
	public function __construct() {
		$this->initVar("event_id", XOBJ_DTYPE_INT, null, false); // 分类ID
		$this->initVar("uid", XOBJ_DTYPE_INT); // 分类ID
		$this->initVar("group_id", XOBJ_DTYPE_INT); // 组ID
		$this->initVar("event_itemid", XOBJ_DTYPE_INT); 
		$this->initVar("event_type", XOBJ_DTYPE_INT); // 事件类型 100+ 用户事件, 200+ 群主事件, 300+管理员事件
		$this->initVar("event_subject", XOBJ_DTYPE_TXTBOX);
		$this->initVar("event_content", XOBJ_DTYPE_TXTAREA);
		$this->initVar("event_create_time", XOBJ_DTYPE_INT);
	}
		
}

class groupsEventHandler extends XoopsPersistableObjectHandler
{
	public function __construct($db) {
        parent::__construct($db,"groups_event","groupsEvent","event_id");
    }
    
    /**
     * create and modify an event
     *
     * @param array $params
     * @return int $id ($event_id)
     */
    public function setEvent( array $params ) {
        $myts =  MyTextSanitizer::getInstance();
        if ( empty($params) || !is_array($params) ) return false;
    	$id = isset($params["event_id"]) ? intval($params["event_id"]) : 0 ;
        if ( !empty($id) ) {
            $obj = $this->get($id);
        } else {
            $obj = $this->get();
            $obj->setVar("event_create_time",time());
        }
    	foreach ( $params as $key => $val ) {
			if ( !isset($obj->vars[$key]) || $myts->stripSlashesGPC($val) == $obj->getVar($key) ) continue;
			$obj->setVar($key, $val);
		}
    	if ( $id = $this->insert($obj) ) {
    	    return $id;
    	}
    	return false;
    }
    
    /**
     * delete an event
     *
     * @param int $event_id
     * @return bool $ret
     */
    public function deleteEvent( $event_id ) {
        
    }
    
    /**
     * select event list
     *
     * @param array $params
     * @param bool $asObj
     * @return array $ret array/object array
     */
    public function getEvents( array $params, $asObj=true ) {
        $criteria = xoGroups::getCriteria($params);
        $fields = ( isset($params["fields"]) && is_array($params["fields"]) ) ? $params["fields"] : null ;
        $ret = $this->getAll($criteria, $fields, $asObj);
        return $ret;
    }
    
    /**
     * for footprint page nav
     *
     * @param array $params
     * @return string $pagenav
     */
    public function getPageNav( array $params ) {
        $pagenav = "";
        $criteria = xoGroups::getCriteria($params);
        $counts = $this->getCount($criteria);
        $extra = isset($params["extra"]) ? trim($params["extra"]) : "";
        if( $counts > $params["limit"]){	
        	$nav = new XoopsPageNav($counts, $params["limit"], $start, "start",$extra);
        	$pagenav = $nav->renderNav(4);
        }
        return $pagenav;
    }
}